package com.qianxx.component.activity.list.swipe_list_view;

import android.os.Bundle;
import android.view.View;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.qianxx.component.R;
import com.qianxx.component.activity.base.BaseActivity;
import com.qianxx.viewlibrary.list.swiplistview.BaseSwipeListViewListener;
import com.qianxx.viewlibrary.list.swiplistview.SwipeListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SwipeListViewActivity extends BaseActivity {
    @BindView(R.id.swipe_list_view)
    SwipeListView swipe_list_view;

    SimpleAdapter adapter;
    List<String> list = new ArrayList<>();
    List<Map<String, Object>> dataList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_swipe_list_view);
        ButterKnife.bind(this);

        initData();
        initView();
    }

    private void initView() {
        adapter = new SimpleAdapter(this, dataList, R.layout.list_item,new String[] {"text", "remove" },
                new int[] {R.id.text, R.id.remove });
        adapter.setViewBinder(new SimpleAdapter.ViewBinder() {
            @Override
            public boolean setViewValue(View view, final Object data,
                                        String textRepresentation) {
                switch (view.getId()) {
                    case R.id.remove:
                        view.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Map<String, Object> removeItem = null;
                                for (Map<String, Object> map : dataList) {
                                    if (map.get("remove").equals(data)) {
                                        removeItem = map;
                                        break;
                                    }
                                }

                                dataList.remove(removeItem);
                                adapter.notifyDataSetChanged();
                                swipe_list_view.closeOpenedItems();
                            }
                        });
                        break;
                    case R.id.text:
                        ((TextView) view).setText((String) data);
                        break;
                }
                return true;
            }
        });
        swipe_list_view.setAdapter(adapter);
        swipe_list_view.setSwipeListViewListener(new BaseSwipeListViewListener() {
            @Override
            public void onChoiceChanged(int position, boolean selected) {
            }

            @Override
            public void onChoiceEnded() {
            }

            @Override
            public void onChoiceStarted() {
            }

            @Override
            public void onClickBackView(int position) {
            }

            @Override
            public void onClickFrontView(int position) {
            }

            @Override
            public void onClosed(int position, boolean fromRight) {
            }

            @Override
            public void onDismiss(int[] arg0) {
            }

            @Override
            public void onFirstListItem() {
            }

            @Override
            public void onLastListItem() {
            }

            @Override
            public void onListChanged() {
            }

            @Override
            public void onMove(int position, float x) {
            }

            @Override
            public void onOpened(int position, boolean toRight) {
            }

            @Override
            public void onStartClose(int position, boolean right) {
            }

            @Override
            public void onStartOpen(int position, int action, boolean right) {
            }
        });
    }

    private void initData() {
        list.add("阿巴斯");
        list.add("奥迪");
        list.add("阿斯顿马丁");
        list.add("宝马");
        list.add("保时捷");
        list.add("奔驰");
        list.add("标致");
        list.add("别克");
        list.add("宾利");
        list.add("比亚迪");
        list.add("长安");
        list.add("大众");
        list.add("福特");
        list.add("海马");
        list.add("皇冠");
        list.add("江淮");
        list.add("捷豹");
        list.add("凯迪拉克");
        list.add("兰博基尼");
        list.add("劳斯莱斯");
        list.add("雷克萨斯");
        list.add("雷诺");
        list.add("力帆");
        list.add("路虎");
        list.add("迈巴赫");
        list.add("马自达");
        list.add("Mini");
        list.add("Nissan");
        list.add("奇瑞");
        list.add("起亚");
        list.add("现代");
        list.add("雪佛兰");
        list.add("雪铁龙");
        list.add("英菲尼迪");
        list.add("中兴");


        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("text", list.get(i));
            map.put("remove", i);
            dataList.add(map);
        }
    }

    @Override
    public String setTitle() {
        return "SwipeListView";
    }
}
